# Set console encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "========================================" -ForegroundColor Green
Write-Host "Auto Upload Project to Gitee" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

# Check if Git is installed
function Test-GitInstalled {
    try {
        $gitVersion = git --version 2>$null
        if ($LASTEXITCODE -eq 0) {
            Write-Host "Git is installed: $gitVersion" -ForegroundColor Green
            return $true
        } else {
            return $false
        }
    } catch {
        return $false
    }
}

# Download and install Git
function Install-Git {
    Write-Host "Downloading Git..." -ForegroundColor Yellow
    
    # Create temp directory
    $tempDir = "$env:TEMP\git_install"
    if (!(Test-Path $tempDir)) {
        New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
    }
    
    # Download Git installer
    $gitUrl = "https://github.com/git-for-windows/git/releases/download/v2.43.0.windows.1/Git-2.43.0-64-bit.exe"
    $installerPath = "$tempDir\GitInstaller.exe"
    
    try {
        Write-Host "Downloading Git installer..." -ForegroundColor Cyan
        Invoke-WebRequest -Uri $gitUrl -OutFile $installerPath -UseBasicParsing
        
        Write-Host "Installing Git..." -ForegroundColor Cyan
        Write-Host "Please follow the default settings in the installation window" -ForegroundColor Yellow
        Write-Host "After installation, please run this script again" -ForegroundColor Yellow
        
        # Start installer
        Start-Process -FilePath $installerPath -Wait
        
        Write-Host "Git installation completed! Please run this script again" -ForegroundColor Green
        Read-Host "Press any key to exit"
        exit 0
        
    } catch {
        Write-Host "Failed to download Git!" -ForegroundColor Red
        Write-Host "Please download Git manually: https://git-scm.com/download/win" -ForegroundColor Yellow
        Read-Host "Press any key to exit"
        exit 1
    }
}

# Initialize Git repository and upload
function Initialize-GitRepository {
    Write-Host "Initializing Git repository..." -ForegroundColor Cyan
    
    # Check if already initialized
    if (Test-Path ".git") {
        Write-Host "Git repository already exists" -ForegroundColor Yellow
    } else {
        git init
        if ($LASTEXITCODE -ne 0) {
            Write-Host "Git initialization failed!" -ForegroundColor Red
            return $false
        }
        Write-Host "Git repository initialized successfully!" -ForegroundColor Green
    }
    
    # Add all files
    Write-Host "Adding files..." -ForegroundColor Cyan
    git add .
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Failed to add files!" -ForegroundColor Red
        return $false
    }
    
    # Commit code
    Write-Host "Committing code..." -ForegroundColor Cyan
    git commit -m "Initial commit: Django project"
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Failed to commit code!" -ForegroundColor Red
        return $false
    }
    
    Write-Host "Local Git repository is ready!" -ForegroundColor Green
    return $true
}

# Main program
Write-Host "Checking Git installation status..." -ForegroundColor Cyan

if (Test-GitInstalled) {
    Write-Host "Git is installed, continuing..." -ForegroundColor Green
} else {
    Write-Host "Git is not installed, downloading and installing..." -ForegroundColor Yellow
    Install-Git
}

# Initialize Git repository
if (Initialize-GitRepository) {
    Write-Host "========================================" -ForegroundColor Green
    Write-Host "Local Git repository is ready!" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Green
    Write-Host ""
    
    Write-Host "Next steps to complete manually:" -ForegroundColor Yellow
    Write-Host ""
    
    Write-Host "1. Login to your Gitee account" -ForegroundColor White
    Write-Host "2. Click '+' in top right corner → 'New Repository'" -ForegroundColor White
    Write-Host "3. Fill in repository information:" -ForegroundColor White
    Write-Host "   - Repository name: mybabys-project" -ForegroundColor White
    Write-Host "   - Description: My Django project" -ForegroundColor White
    Write-Host "   - Select 'Public'" -ForegroundColor White
    Write-Host "   - Do NOT check 'Initialize with README'" -ForegroundColor White
    Write-Host "4. After creating repository, copy the repository URL" -ForegroundColor White
    Write-Host "5. Run these commands in terminal:" -ForegroundColor White
    Write-Host "   git remote add origin [your-repository-url]" -ForegroundColor Green
    Write-Host "   git push -u origin master" -ForegroundColor Green
    Write-Host ""
    
    Write-Host "Or use Gitee Desktop (Recommended):" -ForegroundColor Cyan
    Write-Host "1. Download and install Gitee Desktop" -ForegroundColor White
    Write-Host "2. Open Gitee Desktop" -ForegroundColor White
    Write-Host "3. Select 'New Repository'" -ForegroundColor White
    Write-Host "4. Select current folder" -ForegroundColor White
    Write-Host "5. Fill in information and submit" -ForegroundColor White
    Write-Host ""
    
    Write-Host "Project files are ready:" -ForegroundColor Green
    Write-Host "✅ .gitignore - Ignore unnecessary files" -ForegroundColor Green
    Write-Host "✅ README.md - Project documentation" -ForegroundColor Green
    Write-Host "✅ requirements.txt - Project dependencies" -ForegroundColor Green
    Write-Host "✅ All Django project files" -ForegroundColor Green
    Write-Host ""
    
    Read-Host "Press any key to exit"
} else {
    Write-Host "Git repository initialization failed!" -ForegroundColor Red
    Read-Host "Press any key to exit"
} 